import { ITEM_DATA, type ItemConfig } from '../../data/item';
import { ITEM_TYPE } from '../../data/item/type';
import { type CultivationSkillItemConfig } from '../../data/item/type';
import { CONDITION_TYPE } from '../../systems/condition-checker/type';
import type { CultivationSkill } from './type';

/** 功法管理类 */
export class CultivationSkillManager {
  private playerSkills: Map<string, CultivationSkill> = new Map();
  private configs: Map<string, CultivationSkillItemConfig> = new Map();

  constructor() {
    this.registerConfigs(ITEM_DATA);
  }

  /** 注册功法 */
  public registerConfig(config: CultivationSkillItemConfig): void {
    this.configs.set(config.id, config);
  }

  /** 批量注册功法 */
  public registerConfigs(configs: ItemConfig[]): void {
    configs.forEach((config) => {
      if (config.type === ITEM_TYPE.CULTIVATION_SKILL) {
        this.registerConfig(config as CultivationSkillItemConfig);
      }
    });
  }

  /** 获取功法配置 */
  public getItemConfig(itemId: string): CultivationSkillItemConfig | undefined {
    return this.configs.get(itemId);
  }

  /** 获取已学习的功法 */
  public getPlayerSkill(skillId: string): CultivationSkill | undefined {
    return this.playerSkills.get(skillId);
  }

  /** 设置已学习的功法 */
  public setPlayerSkills(skills: CultivationSkill[]): void {
    this.playerSkills = new Map(skills.map((skill) => [skill.skillId, skill]));
  }

  /** 创建一个新的功法 */
  public createPlayerCultivationSkill(
    skillConfig: CultivationSkillItemConfig
  ): CultivationSkill {
    return {
      skillId: skillConfig.id,
      currentLevel: 1,
      isUnlocked: true,
      isMain: false,
      breakthroughProgress: {},
    };
  }

  /** 学习新功法 */
  public learnSkill(skillId: string): boolean {
    const config = this.getItemConfig(skillId);
    if (!config) {
      return false;
    }

    const cultivationSkill = this.createPlayerCultivationSkill(config);

    this.playerSkills.set(skillId, cultivationSkill);
    return true;
  }

  /** 移除已学习的功法 */
  public removeLearnedCultivationSkill(skillId: string): boolean {
    return this.playerSkills.delete(skillId);
  }

  /** 设置一个功法为主修 */
  public setMainCultivationSkill(skillId: string): boolean {
    const cultivationSkill = this.playerSkills.get(skillId);
    if (!cultivationSkill) return false;
    /** 将其他功法设置为非主修 */
    this.playerSkills.forEach((skill) => {
      skill.isMain = false;
    });
    cultivationSkill.isMain = true;
    return true;
  }

  /** 是否有主修功法 */
  public hasMainCultivationSkill(): boolean {
    return Array.from(this.playerSkills.values()).some((skill) => skill.isMain);
  }

  public isLearned(skillId: string): boolean {
    return this.playerSkills.has(skillId);
  }

  /** 获取已学习的功法详情列表 */
  public getPlayerSkillsInfo() {
    return Array.from(this.playerSkills.values())
      .map((skill) => {
        const config = this.getItemConfig(skill.skillId);
        if (!config) return null;
        return {
          skill,
          skillConfig: config,
        };
      })
      .filter((v) => v !== null);
  }

  /** 升级功法 */
  public upgradeSkill(skillId: string): boolean {
    const cultivationSkill = this.playerSkills.get(skillId);
    if (!cultivationSkill) return false;
    cultivationSkill.currentLevel += 1;
    return true;
  }

  /** 获取已学功法当前等级所需突破条件 */
  public getBreakthroughCondition(skillId: string) {
    const cultivationSkill = this.playerSkills.get(skillId);
    if (!cultivationSkill) return undefined;
    const currentLevelConfig = this.getItemConfig(cultivationSkill.skillId)
      ?.levels[cultivationSkill.currentLevel - 1];
    if (!currentLevelConfig) return undefined;
    return currentLevelConfig.breakthroughCondition;
  }

  /** 更新击杀敌人  */
  public updateCultivationKillEnemies(enemyIds: string[]) {
    for (const value of this.playerSkills.values()) {
      const breakthroughCondition = this.getBreakthroughCondition(
        value.skillId
      );
      if (!breakthroughCondition) continue;
      breakthroughCondition.forEach((condition) => {
        if (condition.type === CONDITION_TYPE.KILL_MONSTER) {
          const monsterId = condition.monsterId;
          if (!monsterId) return;
          if (enemyIds.includes(monsterId)) {
            if (!value.breakthroughProgress) {
              value.breakthroughProgress = {};
            }
            if (value.breakthroughProgress[monsterId]) {
              value.breakthroughProgress[monsterId] += 1;
            } else {
              value.breakthroughProgress[monsterId] = 1;
            }
          }
        }
      });
    }
  }

  /** 重置某个已学技能的进度（突破成功后需要清除） */
  public resetCultivationSkillProgress(skillId: string) {
    const cultivationSkill = this.playerSkills.get(skillId);
    if (!cultivationSkill) return false;
    cultivationSkill.breakthroughProgress = {};
    return true;
  }

  /** 获取已学习功法的数量 */
  public getLearnedCultivationSkillCount() {
    return this.playerSkills.size;
  }

  /** 功法是否已学习 */
  public isCultivationSkillLearned(skillId: string) {
    return this.playerSkills.has(skillId);
  }
}
